//
//  LoginCtrl.m
//  iOS
//
//  Created by LiuHe@MacMini on 14-7-14.
//  Copyright (c) 2014年 cloudyoo. All rights reserved.
//

#import "LoginCtr.h"
#import "RegisterCtr.h"
#import "MainCtr.h"
#import "AppDelegate.h"
#import "User.h"

@interface LoginCtrl ()

@property (nonatomic,strong) UITextField *userNameTextField;
@property (nonatomic,strong) UITextField *passwordTextField;

@end

@implementation LoginCtrl

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self UIInit];
}

-(void)UIInit
{
    self.title = @"登录";
    [self setRightItem:@"注册"];
    
    //头像
    UIImageView *avatorImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 57, 57)];
    avatorImgView.backgroundColor = [UIColor whiteColor];
    [avatorImgView setImage:[UIImage imageNamed:@"default_avatar.png"]];
    avatorImgView.layer.cornerRadius = avatorImgView.frame.size.width/2;
    [self.sView addSubviewToHorizontalCenter:avatorImgView];
    
    UIView *inputBg = [[UIView alloc]initWithFrame:CGRectMake(0,avatorImgView.bottom+20, screen_w, 44*2)];
    [inputBg setBackgroundColor:[UIColor whiteColor]];
    inputBg.layer.borderColor = sa_color(214, 1.0).CGColor;
    inputBg.layer.borderWidth = 1.0;
    [self.sView addSubview:inputBg];
    
    //用户名
    UIImageView *userNameFlag = [[UIImageView alloc]initWithFrame:CGRectMake(15, 15, 14, 14)];
    [userNameFlag setImage:[UIImage imageNamed:@"login_uname.png"]];
    [inputBg addSubview:userNameFlag];
    
    _userNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 0, screen_w-55, 44)];
    _userNameTextField.delegate = self;
    _userNameTextField.tag = 1;
    _userNameTextField.returnKeyType = UIReturnKeyNext;
    _userNameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _userNameTextField.placeholder = [NSString stringWithFormat:@"账号"];
    _userNameTextField.font = [UIFont systemFontOfSize:14];
    _userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

    [inputBg addSubview:_userNameTextField];
    
    UILabel *lineA = [[UILabel alloc]initWithFrame:CGRectMake(15, 44, screen_w-15, 1)];
    lineA.backgroundColor = sa_color(214, 1.0);
    [inputBg addSubview:lineA];
    
    //密码
    UIImageView *passwordFlag = [[UIImageView alloc]initWithFrame:CGRectMake(userNameFlag.left, 59, 14, 14)];
    [passwordFlag setImage:[UIImage imageNamed:@"login_pwd.png"]];
    [inputBg addSubview:passwordFlag];

    _passwordTextField = [[UITextField alloc]initWithFrame:CGRectMake(40, 44, screen_w-55, 44)];
    _passwordTextField.delegate = self;
    _passwordTextField.tag = 2;
    _passwordTextField.returnKeyType = UIReturnKeySend;
    _passwordTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passwordTextField.placeholder = [NSString stringWithFormat:@"密码"];
    _passwordTextField.font = [UIFont systemFontOfSize:14];
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [inputBg addSubview:_passwordTextField];
    
    
    UIButton *loginBt = [[UIButton alloc] initWithSystemTheme:CGRectMake(0, inputBg.bottom+40, screen_w-40, 44) title:@"登 录" target:self action:@selector(clickLogin)];
    [self.sView addSubviewToHorizontalCenter:loginBt];
    
    
    //忘记密码
    UIButton *forgetBt = [[UIButton alloc]initWithFrame:CGRectMake(20, 0, 100, 44)];
    forgetBt.backgroundColor = [UIColor clearColor];
    forgetBt.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [forgetBt setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
    [forgetBt setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [forgetBt addTarget:self action:@selector(clickForgetPassword) forControlEvents:UIControlEventTouchUpInside];
    forgetBt.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.sView addSubviewToBottomAlign:forgetBt];
    
    //立即注册
    UIButton *registeBtn = [[UIButton alloc]initWithFrame:CGRectMake(screen_w-20-100, 0, 100, 44)];
    registeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    registeBtn.backgroundColor = [UIColor clearColor];
    [registeBtn setTitleColor:[UIColor themeColor] forState:UIControlStateNormal];
    [registeBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    [registeBtn addTarget:self action:@selector(clickRegist) forControlEvents:UIControlEventTouchUpInside];
    registeBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.sView addSubviewToBottomAlign:registeBtn];
}

-(void)rightItemClick:(UIButton *)sender{
    [self clickRegist];
}

-(void)textFieldReturn:(UITextField *)textField{
    [self clickLogin];
}

-(void)clickLogin
{
    if (![self checkData]) {
        return;
    }
    [AlertUtils showLoadingToast:@"登录中"];
    [[User new] loginWithUname:_userNameTextField.text pwd:_passwordTextField.text ok:^(HttpResponse *response, id responseObj) {
        [AlertUtils hideToast];
        if (response.code == http_code_ok) {
            [self goMain];
        }
    } no:^(HttpResponse *response, NSString *errMsg) {
        [AlertUtils showToast:errMsg];
    }];
}

-(void)goMain{
    MainCtr *ctr = [[MainCtr alloc] initWithNibName:nil bundle:nil];
    [((AppDelegate *)[UIApplication sharedApplication].delegate) setNavigationRootViewController:ctr];;
}

//数据校验
-(BOOL)checkData
{
    //验证是否为邮箱或者手机号码
    if (str_isblank(_userNameTextField.text)) {
        [_userNameTextField becomeFirstResponder];
        [AlertUtils showToast:@"请输入账号"];
        return NO;
    }
    
    if (str_isblank(_passwordTextField.text)) {
        [_userNameTextField becomeFirstResponder];
        [AlertUtils showToast:@"请输入密码"];
        return NO;
    }
    return YES;
}
#pragma mark 忘记密码
-(void)clickForgetPassword
{
    
}
#pragma mark 注册
- (void)clickRegist
{
    RegisterCtr *registerCtr = [[RegisterCtr alloc]init];
    [self.navigationController pushViewController:registerCtr animated:YES];
}


@end
